<template>
  <div class="edit-box">
    <el-form :model="form" :inline="true" label-position="top" :rules="formRules" ref="ruleForm">
      <el-form-item label="类型" prop="customerType">
        <el-select v-model="form.customerType" placeholder="请选择">
          <el-option v-for="item in typeOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属类别" prop="allTypeId">
        <el-cascader
          v-model="form.allTypeId"
          :show-all-levels="false"
          :options="typeList"
          :props="{value: 'id',label: 'name', checkStrictly: true, emitPath: false}"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="名称" prop="customerName">
        <el-input v-model="form.customerName"></el-input>
      </el-form-item>
      <el-form-item label="联系人">
        <el-input v-model="form.leadingName"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="leadingPhone">
        <el-input v-model="form.leadingPhone"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="leadingEmail">
        <el-input v-model="form.leadingEmail"></el-input>
      </el-form-item>
      <el-form-item style="width:100%">
        <el-checkbox v-model="form.star" :true-label="1" :false-label="0">设为常用</el-checkbox>
      </el-form-item>
      <el-form-item style="width:100%">
        <el-checkbox v-model="form.invoice" :true-label="1" :false-label="0">开票信息</el-checkbox>
      </el-form-item>
      <div v-show="form.invoice">
        <el-form-item label="抬头">
          <el-input v-model="form.invoiceName"></el-input>
        </el-form-item>
        <el-form-item label="税号">
          <el-input v-model="form.invoiceNum"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="form.address"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.bankCardPhone"></el-input>
        </el-form-item>
        <el-form-item label="开户行">
          <el-input v-model="form.openingBank"></el-input>
        </el-form-item>
        <el-form-item label="账号">
          <el-input v-model="form.bankCardId"></el-input>
        </el-form-item>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('changeState',false)">取 消</el-button>
      <el-button type="primary" @click="submit">保存</el-button>
    </div>
  </div>
</template>
<script>
import { update, findById } from '@/api/customer'
import { findList } from '@/api/type'
export default {
  data() {
    let phoneValid = (rule, value, callback) => {
      if (value !== '') {
        let reg = /^1[3|4|5|7|8][0-9]{9}$/
        if (!reg.test(value)) {
          callback(new Error('请输入正确的手机号'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      form: {
        id: '',
        companyId: this.$store.state.user.companyId,
        customerType: '',
        customerName: '',
        leadingName: '',
        leadingPhone: '',
        leadingEmail: '',
        customerTelPhone: '',
        star: 0,
        invoice: 0,
        invoiceName: '',
        invoiceNum: '',
        openingBank: '',
        bankCardId: '',
        address: '',
        bankCardPhone: '',
        allTypeId: ''
      },
      typeList: [],
      typeOptions: [
        {
          id: '0',
          name: '客户'
        },
        {
          id: '1',
          name: '供应商'
        },
        {
          id: '2',
          name: '客户/供应商'
        }
      ],
      formRules: {
        customerType: [
          { required: true, message: '请输入客户类型', trigger: 'blur' }
        ],
        customerName: [
          { required: true, message: '请输入客户名称', trigger: 'blur' }
        ],
        leadingEmail: [
          {
            type: 'email',
            message: '请输入正确的邮箱地址',
            trigger: ['blur', 'change']
          }
        ],
        leadingPhone: [{ validator: phoneValid, trigger: 'blur' }]
      }
    }
  },
  beforeDestroy() {
    this.$off('reload')
    this.$off('changeState')
  },
  mounted() {
    this.initTypeOptions()
  },
  methods: {
    initTypeOptions() {
      findList(this.form.companyId, 2).then(res => {
        this.typeList = res.data
      })
    },
    initDialogData(id) {
      findById(id).then(res => {
        this.form = res.data
      })
    },
    submit() {
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          update(this.form).then(res => {
            if (res.success) {
              this.$emit('reload')
              this.$emit('changeState', false)
              this.$message.success(res.message)
            } else {
              this.$message.error(res.message)
            }
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.edit-box {
  padding: 0 50px;
  height: 550px;
  overflow-y: auto;
}
.dialog-footer {
  text-align: right;
}
.el-form--inline .el-form-item {
  width: calc(50% - 12px);
}
</style>
